import { Request, Response, Router, Handler } from 'express';
import * as express from 'express';

const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';
type Method = 'GET'|'POST'|'PUT'|'DELETE';

interface RouterMiddlewareOptions {
  path: string;
  method: Method;
};

export const createRouterMiddleware = (method: Method, path: string, object:any):Router => {

  let handler: Handler ;
  if (typeof object == 'function') {
    handler = object;
  } else {
    handler = createRouterHandler(object);
  }

  const router: Router = createRouterByMethod(method, path, handler);
  return router;
}

/**
 * 创建处理所有动词对应的请求的处理器
 * @param path 请求路径
 * @param handler 请求处理器
 */
export const createAllRouter = (path: string, handler: Handler): Router => {
  const router = express.Router();
  return router.all(path, handler);
}
/**
 * 创建处理Get请求的路由
 * @param path 请求路径
 * @param handler 请求处理器
 */
export const createGetRouter = (path: string, handler: Handler): Router => {
  const router = express.Router();
  return router.get(path, handler);
}

/**
 * 创建处理POST请求的路由
 * @param path 请求路径
 * @param handler 请求处理器
 */
export const createPostRouter = (path: string, handler: Handler): Router => {
  const router = express.Router();
  return router.post(path, handler);
}

/**
 * 创建处理Put请求的路由
 * @param path 请求路径
 * @param handler 请求处理器
 */
export const createPutRouter = (path: string, handler: Handler): Router => {
  const router = express.Router();
  return router.put(path, handler);
}

/**
 * 创建处理Delete请求的路由
 * @param path 请求路径
 * @param handler 请求处理器
 */
export const createDeleteRouter = (path: string, handler: Handler): Router => {
  const router = express.Router();
  return router.delete(path, handler);
}

/**
 * 根据响应内容创建请求处理器
 * @param body 响应内容
 */
export const createRouterHandler = (body:any): Handler => {
  return (req: Request, res: Response) => {
    res.send(body);
  };
}

/**
 * 根据请求动词创建对应的路由
 * @param method 请求动词
 * @param path 请求路径
 * @param handler 请求处理器
 */
export const createRouterByMethod = (method: Method, path: string, handler: Handler):Router => {
  let router: Router;
  switch (method) {
    case 'GET':
      router = createGetRouter(path, handler);
      break;
    case 'POST':
      router = createPostRouter(path, handler);
      break;
    case 'PUT':
      router = createPutRouter(path, handler);
      break;
    case 'DELETE':
      router = createPutRouter(path, handler);
      break;
    default:
      router = createAllRouter(path, handler);
      break;
  }
  return router;
}

/**
 * 从待解析的字符串中解析出对应的动词和路径。
 * 该方法将返回RouterMiddlewareOptions对象，默认method为GET。
 * @param key 待解析的字符串
 * @param regex 匹配规则
 */
export const splitMethodAndPath = (key: string, regex: string|RegExp):RouterMiddlewareOptions => {
  const substrings: string[] = key.split(regex);
  const length = substrings.length;

  let result: RouterMiddlewareOptions = { method: 'GET', path: ''};
  if (length == 1) {
    result.method = GET;
    result.path = substrings[0];
  } else if (length == 2) {
    const method = substrings[0];
    if (method === GET ||POST === method || PUT === method || DELETE === method) {
      result.method = method;
    } else {
      result.method = GET;
    }

    result.path = substrings[1];   
  } else {
    throw new Error("got more than 2 substring.");
  }

  return result;
}